package project.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import project.entity.TJdsp;
import project.entity.TLbt;
import project.service.TJdspService;
import project.util.ResponseUtil;
import project.util.UUIDUtil;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 景点商品表 前端控制器
 * </p>
 *
 * @author YJR
 * @since 2023-07-26
 */
@Slf4j
@RestController
@RequestMapping("/api/jdsp/v1")
public class TJdspController {
    @Resource
    private TJdspService jdspService;

    /**
     * 添加或者更景点商品
     * @param jdsp
     * @return
     */
    @RequestMapping(value="/addOrUpdate",method = RequestMethod.POST)
    public JSONObject addOrUpdate(@RequestBody TJdsp jdsp) {
        try {
            if (StringUtils.isBlank(jdsp.getcId())) {
                jdsp.setcId(UUIDUtil.getUUID());
            }
            jdspService.saveOrUpdate(jdsp);
            return ResponseUtil.getSuccessResponseBody(null);
        } catch (Exception e) {
            log.error("addOrUpdate出现异常", e);
            return ResponseUtil.getErrorResponseBody(null, "服务出现异常");
        }
    }

    @RequestMapping(value="wx/select",method = RequestMethod.POST)
    public List<TJdsp> wxSelect(@RequestBody JSONObject param) {
        String jdid = param.getString("jdid");
        QueryWrapper<TJdsp> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(jdid)){
            wrapper.lambda().eq(TJdsp::getcJdId,jdid);
        }
       return jdspService.list(wrapper);
    }
    /**
     *  分页查询数据
     */
    @RequestMapping(value="/select",method = RequestMethod.POST)
    public JSONObject selectSpfl(@RequestBody JSONObject param){
        // 获取页数
        Integer current = param.getInteger("current");
        // 获取景点id
        String cJdId = param.getString("cJdId");
        // 景点商品名称
        String cName = param.getString("cName");

        Page<TJdsp> page = new Page<TJdsp>();
        page.setSize(10);
        if(current != null){
            page.setCurrent(current);
        }else{
            page.setCurrent(1);
        }
        QueryWrapper<TJdsp> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(cJdId)){
            wrapper.lambda().eq(TJdsp::getcJdId,cJdId);
        }
        if(StringUtils.isNotBlank(cName)){
            wrapper.lambda().like(TJdsp::getcName,cName);
        }
        // 获取查询返回结果
        Page<TJdsp> pageSelect = jdspService.page(page, wrapper);
        JSONObject object = new JSONObject();
        object.put("list",pageSelect.getRecords());
        object.put("total",pageSelect.getTotal());
        return ResponseUtil.getSuccessResponseBody(object);
    }

    /**
     * 删除操作
     * @param jdsp
     * @return
     */
    @RequestMapping(value = "/del",method = RequestMethod.POST)
    public JSONObject del(@RequestBody TJdsp jdsp){
        return ResponseUtil.getSuccessResponseBody(jdspService.removeById(jdsp));
    }
}
